//
//  LJLProvincePageVC.m
//  聆听FM
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 Dark. All rights reserved.
//
#import "LTNetTool.h"
#import "LJLRadioPageModel.h"

#import "LJLProvincePageVC.h"
#import "LJLRadioPageLocalVC.h"
#import "ViewController.h"
#import "TYTabButtonPagerController.h"
@interface LJLProvincePageVC ()<TYPagerControllerDataSource>

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) TYTabButtonPagerController *pagerController;

@end

@implementation LJLProvincePageVC
- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    _pagerController.view.frame = self.view.bounds;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"省市台";
    self.dataSource = [NSMutableArray array];
    self.navigationController.navigationBar.translucent = YES;
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.navigationController.navigationBar.hidden = NO;
    [self createData];
    [self createView];
}

- (void)createView
{
    self.pagerController = [[TYTabButtonPagerController alloc]init];
    _pagerController.dataSource = self;
    _pagerController.adjustStatusBarHeight = YES;
    _pagerController.barStyle = TYPagerBarStyleProgressView;
    _pagerController.cellSpacing = 8;
    _pagerController.progressWidth = 0;
    _pagerController.view.frame = self.view.bounds;
    [self addChildViewController:_pagerController];
    [self.view addSubview:_pagerController.view];
}
- (void)createData
{
    [LTNetTool GetNetWithURL:@"http://live.ximalaya.com/live-web/v1/getProvinceList" Body:nil Header:nil Response:0 Success:^(id result) {
        NSArray *array = [result objectForKey:@"result"];
        for (NSDictionary *dic in array) {
            LJLRadioPageModel *model = [[LJLRadioPageModel alloc] initWithDic:dic];
            [_dataSource addObject:model];
        }
        if (self.dataSource.count) {
            [self.pagerController reloadData];
        }
    } Failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (NSInteger)numberOfControllersInPagerController
{
    if (self.dataSource.count == 0) {
        return 1;
    }
    return self.dataSource.count;
}
- (NSString *)pagerController:(TYPagerController *)pagerController titleForIndex:(NSInteger)index
{
    if (self.dataSource.count == 0) {
        return @"";
    }
    LJLRadioPageModel *model = [self.dataSource objectAtIndex:index];
    return model.provinceName;
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index
{
    if (self.dataSource.count == 0) {
            ViewController *rplVC = [[ViewController alloc] init];
        return rplVC;
    }
    LJLRadioPageModel *model = [self.dataSource objectAtIndex:index];
    LJLRadioPageLocalVC *rplVC = [[LJLRadioPageLocalVC alloc] init];
    rplVC.provinceCode = model.provinceCode;
    rplVC.type = 2;
    rplVC.style = 1;
    return rplVC;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
